﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataBaseTable;
using MySpace.Data;
using System.Data;

public partial class WishZT : System.Web.UI.Page
{
    protected static int nowpages;
    protected int pagesize = 20;
    protected int wall_width = 960;
    protected int wall_height = 600;
    protected int daySpace;
    protected WebTable web = new WebTable();
    protected string pZhuanTiName = "";
    protected string pBackground = "";
    protected string pBunnerImg = "";
    protected string pFontColor;
    protected string pConBackground;
    protected Random rd = new Random();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //zhuantiID_hf.Value = Request.QueryString["id"];
            zhuantiID_hf.Value = "6";
            nowpages = 1;
            this.bdnode();
        }
        this.PageInit();
    }
    private void PageInit()
    {
        WishZhuanTiTable wztt = new WishZhuanTiTable(Convert.ToInt32(zhuantiID_hf.Value));
        if (wztt.ID == 0)
            Response.Redirect("~/__errfiles__/404.html");
        this.pZhuanTiName = wztt.ZhuanTiName;
        this.pBackground = ResolveUrl(wztt.Background.Replace("$path$", ResolveUrl("~/WillZTimgaes/")));
        this.pBunnerImg=ResolveUrl("~/WillZTimgaes/"+wztt.BunnerImg);
        this.pFontColor="#"+wztt.FontColor;
        this.pConBackground = ResolveUrl(wztt.ConBackground.Replace("$path$", ResolveUrl("~/WillZTimgaes/")));
        DateTime time1 = DateTime.Now;
        DateTime time2 = DateTime.Parse(wztt.ZhuanTiData);
        this.daySpace = (time2 - time1).Days;
    }
    private void bdnode()
    {
        if (!IsPostBack)
        {
            this.initDropDownList();
        }
        DBHandler dbh = GetHandler.Init();
        dbh.CommandText = string.Format("select * from WishingWall where ztID={0} order by id desc", zhuantiID_hf.Value);
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = dbh.ExecuteDataTable().DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = pagesize;
        toone.Enabled = true;
        toup.Enabled = true;
        tonext.Enabled = true;
        tolast.Enabled = true;
        if (nowpages == 1)
        {
            toone.Enabled = false;
            toup.Enabled = false;
        }
        if (nowpages == ps.PageCount)
        {
            tonext.Enabled = false;
            tolast.Enabled = false;
        }
        if (!IsPostBack)
        {
            for (int i = 1; i <= ps.PageCount; i++)
            {
                jumppage.Items.Add(i.ToString());
            }
        }
        curpage.Text = nowpages.ToString();
        sumpage.Text = ps.PageCount.ToString();
        tpagesizes.Text = ps.PageSize.ToString();
        pagesum.Text = ps.DataSourceCount.ToString();
        ps.CurrentPageIndex = nowpages - 1;//获取当前页码
        nodelist_rp.DataSource = ps;
        nodelist_rp.DataBind();
    }
    protected void toone_Click(object sender, EventArgs e)
    {
        nowpages = 1;
        this.bdnode();
    }
    protected void toup_Click(object sender, EventArgs e)
    {
        --nowpages;
        this.bdnode();
    }
    protected void tonext_Click(object sender, EventArgs e)
    {
        ++nowpages;
        this.bdnode();
    }
    protected void jumppage_SelectedIndexChanged(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(((DropDownList)sender).SelectedValue);
        this.bdnode();
    }
    protected void tolast_Click(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(sumpage.Text);
        this.bdnode();
    }
    private void initDropDownList()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath(@"~/XML/BlessingType.xml"));
        lei_ddl.DataSource = ds;
        lei_ddl.DataTextField = "value";
        lei_ddl.DataValueField = "id";
        lei_ddl.DataBind();
    }
    protected void submit_btn_Click(object sender, EventArgs e)
    {
        if (!ycode_tb.Text.ToLower().Equals(Session["CheckCode"].ToString().ToLower()))
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('验证码不正确');</script>");
            return;
        }
        WishingWallTable wwt = new WishingWallTable();
        wwt.qname = name_tb.Text;
        wwt.qtname = toname_tb.Text;
        wwt.qqq = qq_tb.Text;
        wwt.qlei = lei_ddl.SelectedValue;
        wwt.qgame = game_tb.Text;
        wwt.qip = GetClientIP.get();
        wwt.qshenhe = 1;
        wwt.ztID = Convert.ToInt32(zhuantiID_hf.Value);
        try
        {
            wwt.Insert();
            name_tb.Text = "匿名";
            toname_tb.Text = string.Empty;
            qq_tb.Text = string.Empty;
            lei_ddl.SelectedIndex = 0;
            game_tb.Text = string.Empty;
            ycode_tb.Text = string.Empty;
            nowpages = 1;
            this.bdnode();
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('您许愿成功!!\\n首先谢谢您来到这里留下您的祝福!\\n我在这里祝您以后心想事成,愿望成真!!');</script>");
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('抱歉由于系统原因,发送失败');</script>");
        }
    }
}